<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PersonInfo extends Model
{
    protected $table = 'users';
    public $timestamps = false;
    protected $guarded = [
        'id', 'nickname', 'avatar', 'totalPoints', 'presentPoints', 'created_at', 'updated_at'
    ];

    public function getData($search = [], $page = 1, $length = 20){
        $usersList = array();
        $usersList['pageData'] = array();
        $date = date('Y-m-d H:i:s', time());

        $sql = \DB::table($this->table)
            ->whereNull('deleted_at');

        if(isset($search['nickname'])){
            $sql->where('nickname', 'like', '%'.$search['nickname'].'%');
        }
        if(isset($search['id'])){
            $sql->where('id', 'like', '%'.$search['id'].'%');
        }

        $usersList['pageData']['count']     = $sql->count();
        $usersList['pageData']['lastPage']  = ceil($usersList['pageData']['count'] / $length);

        $usersList['list'] = $sql->orderBy('totalPoints', 'desc')
            ->skip(($page-1) * $length)->take($length)
            ->get();
        return $usersList;
    }

    public function getDatapoint($search = [], $page = 1, $length = 20){
        $usersList = array();
        $usersList['pageData'] = array();
        $date = date('Y-m-d H:i:s', time());

        $sql = \DB::table('pointsfor as j')
            ->select('p.nick_name','j.iscashback','j.user_id','j.id','j.points','j.money','j.created_at','j.withdrawal_address')
            ->leftjoin('users as p','p.id','=','j.user_id')
            ->where('j.iscashback',0)
            ->whereNull('j.deleted_at');

        if(isset($search['nickname'])){
            $sql->where('nickname', 'like', '%'.$search['nickname'].'%');
        }
        if(isset($search['id'])){
            $sql->where('id', 'like', '%'.$search['id'].'%');
        }

        $usersList['pageData']['count']     = $sql->count();
        $usersList['pageData']['lastPage']  = ceil($usersList['pageData']['count'] / $length);

        $usersList['list'] = $sql->orderBy('j.created_at')
            ->skip(($page-1) * $length)->take($length)
            ->get();
        return $usersList;
    }

    public function saveData($data){
        return \DB::table($this->table)->insert($data);
    }
 //编辑返现状态
    public function setpoints($id,$data){
        return \DB::table('pointsfor')->where('id',$id)->update($data);
    }


    public function setData($id,$data){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }

    public function delData($id,$data){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }
}